package com.zhuwhao.sample;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.zhuwhao.sample.entity.OpenSource;
import com.zhuwhao.sample.utils.AppUtils;
import com.zhuwhao.sample.utils.DensityUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OpenSourceActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<OpenSource> listItems = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_source);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        toolbar.setTitle(R.string.open_source);
        setSupportActionBar(toolbar);

        CommonAdapter<OpenSource> adapter = new CommonAdapter<OpenSource>(this, R.layout.item_open_source, listItems) {
            @Override
            protected void convert(ViewHolder holder, OpenSource openSource, int position) {
                holder.setText(R.id.textName, openSource.getName());
                holder.setText(R.id.textAuthor, openSource.getAuthor());
                holder.setText(R.id.textDescription, openSource.getDescription());
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                AppUtils.openUrl(OpenSourceActivity.this, listItems.get(position).getUrl());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        HeaderAndFooterWrapper wrapper = new HeaderAndFooterWrapper(adapter);
        View view = new View(this);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(this, 8)));
        wrapper.addFootView(view);
        recyclerView.setAdapter(wrapper);
    }

    private void initData() {

        OpenSource pinYin4j = new OpenSource();
        pinYin4j.setName("pinyin4j");
        pinYin4j.setAuthor("belerweb");
        pinYin4j.setDescription("A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.");
        pinYin4j.setUrl("https://github.com/belerweb/pinyin4j");

        OpenSource waveSideBar = new OpenSource();
        waveSideBar.setName("WaveSideBar");
        waveSideBar.setAuthor("gjiazhe");
        waveSideBar.setDescription("An Index Side Bar With Wave Effect");
        waveSideBar.setUrl("https://github.com/gjiazhe/WaveSideBar");

        OpenSource statusBar = new OpenSource();
        statusBar.setName("StatusBarUtil");
        statusBar.setAuthor("laobie");
        statusBar.setDescription("A util for setting status bar style on Android App.");
        statusBar.setUrl("https://github.com/laobie/StatusBarUtil");

        OpenSource butterKnife = new OpenSource();
        butterKnife.setName("ButterKnife");
        butterKnife.setAuthor("JakeWharton");
        butterKnife.setDescription("Bind Android views and callbacks to fields and methods.");
        butterKnife.setUrl("https://github.com/JakeWharton/butterknife");

        OpenSource timePickerDialog = new OpenSource();
        timePickerDialog.setName("TimePickerDialog");
        timePickerDialog.setAuthor("JZXiang");
        timePickerDialog.setDescription("An Android time picker library.");
        timePickerDialog.setUrl("https://github.com/JZXiang/TimePickerDialog");

        OpenSource slidr = new OpenSource();
        slidr.setName("Slidr");
        slidr.setAuthor("r0adkll");
        slidr.setDescription("Easily add slide to dismiss functionality to an Activity.");
        slidr.setUrl("https://github.com/r0adkll/Slidr");

        OpenSource baseAdapter = new OpenSource();
        baseAdapter.setName("BaseAdapter");
        baseAdapter.setAuthor("hongyangAndroid");
        baseAdapter.setDescription("Android 万能的Adapter for ListView,RecyclerView,GridView等，支持多种Item类型的情况。");
        baseAdapter.setUrl("https://github.com/hongyangAndroid/baseAdapter");

        listItems.add(pinYin4j);
        listItems.add(waveSideBar);
        listItems.add(statusBar);
        listItems.add(butterKnife);
        listItems.add(timePickerDialog);
        listItems.add(slidr);
        listItems.add(baseAdapter);

        Collections.sort(listItems, new Comparator<OpenSource>() {
            @Override
            public int compare(OpenSource o1, OpenSource o2) {
                return o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}